package cn.ccit.DAOImp;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.EnumSummaryDAO;
import cn.ccit.entites.EnumSummary;
import cn.ccit.entites.record.CancelDishRecord;
import cn.ccit.util.MyDBUtil;

public class EnumSummaryDAOimp implements EnumSummaryDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(EnumSummary.class);
	@Override
	public boolean addEnumSummary(EnumSummary es) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(es);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("add EnumSummary fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public boolean removeEnumSummary(EnumSummary es) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(es);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove EnumSummary fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public boolean changeEnumSummaryInfo(EnumSummary es) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(es);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("change EnumSummary fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public List<EnumSummary> getSomeEnumSummaries(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from EnumSummary as enumSummary order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from EnumSummary").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeEnumSummaries fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public EnumSummary searchEnumSummaryByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from EnumSummary as enumSummary where enumSummary.id=? ";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				EnumSummary enumSummary=(EnumSummary)query.list().get(0);
				session.beginTransaction().commit();
				return enumSummary; 
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search EnumSummaryByID fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public List<EnumSummary> getEnumByEnumType(String et) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from EnumSummary as enumSummary where enumSummary.name=?";
				Query query=session.createQuery(hql);
				query.setString(0, et);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search getEnumByEnumType fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
}
